package com.jiayi.web.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.jiayi.api.*;
import com.jiayi.common.core.domain.AjaxResult;
import com.jiayi.common.core.page.TableDataInfo;
import com.jiayi.data.entity.request.JiayiNewsSearchPageRequest;
import com.jiayi.data.entity.request.JiayiProductSearchPageRequest;
import com.jiayi.data.entity.response.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@Controller
@RequestMapping
@RequiredArgsConstructor
public class IndexController {

    private final JiayiAboutUsService aboutUsService;
    private final JiayiProductTypeService productTypeService;
    private final JiayiProductService productService;
    private final JiayiNewsTypeService newsTypeService;
    private final JiayiNewsService newsService;
    private final JiayiProductService jiayiProductService;
    private final JiayiNewsService jiayiNewsService;

    @GetMapping(value = {"/","/index","index.html","index.do","index.htm", "/index.vue"})
    public String index(){
        return "index";
    }

    @GetMapping("/index/data")
    @ResponseBody
    public AjaxResult getData() {
        AjaxResult ajaxResult = AjaxResult.success();
        //轮播图
        JiayiAboutUsDTO homeImage = aboutUsService.getInfoByKey("home_image", null);
        ajaxResult.put("homeImage", JSONUtil.parseArray(homeImage.getValueObj()).stream().map(o -> JSONUtil.parseObj(o).getStr("url")).collect(Collectors.toList()));
        //产品展示类型
        ajaxResult.put("productMenu", parseJiayiProductTypeList(productTypeService.selectJiayiProductTypeAncestryList()));
        //新闻动态类型
        ajaxResult.put("newsMenu", getNewsMenu().data());
        //可展示的新闻
        List<JiayiNewsDTO> newsDTOList = newsService.selectJiayiNewsHomeList();
        ajaxResult.put("newsList", newsDTOList);
        return ajaxResult;
    }

    @GetMapping("/index/get-productMenu")
    @ResponseBody
    public AjaxResult getProductMenu() {
        AjaxResult ajaxResult = AjaxResult.success();
        //产品展示类型
        List<JiayiProductTypeDTO> productTypeDTOS = productTypeService.selectJiayiProductTypeListByIds();
        return ajaxResult.data(productTypeDTOS);
    }

    private List<JSONObject> parseJiayiProductTypeList(List<JiayiProductTypeDTO> productTypeDTOS){
        return productTypeDTOS.stream().map(o -> JSONUtil.createObj().set("id", o.getId()).set("name", o.getName()).set("poster", o.getPoster()).set("info", o.getRemark())).collect(Collectors.toList());
    }

    @GetMapping("/index/get-product-info/{productId}")
    @ResponseBody
    public AjaxResult getProductInfo(@PathVariable Long productId) {
        AjaxResult ajaxResult = AjaxResult.success();
        JiayiProductDTO productDTO = productService.selectJiayiProductById(productId);
        return ajaxResult.data(productDTO);
    }

    @GetMapping("/index/get-newsMenu")
    @ResponseBody
    public AjaxResult getNewsMenu() {
        AjaxResult ajaxResult = AjaxResult.success();
        //新闻展示类型
        List<JiayiNewsTypeDTO> jiayiNewsTypeDTOS = newsTypeService.selectJiayiNewsTypeListByIds();
        return ajaxResult.data(jiayiNewsTypeDTOS.stream().map(o ->
                JSONUtil.createObj().set("id", o.getId()).set("name", o.getName())).collect(Collectors.toList()));
    }

    @GetMapping("/index/get-news-info/{newsId}")
    @ResponseBody
    public AjaxResult getNewsInfo(@PathVariable Long newsId) {
        AjaxResult ajaxResult = AjaxResult.success();
        JiayiNewsDTO jiayiNewsDTO = newsService.selectJiayiNewsById(newsId);
        return ajaxResult.data(jiayiNewsDTO);
    }

    /**
     * 关于我们相关内容
     */
    @GetMapping("/index/about-us-data")
    @ResponseBody
    public AjaxResult getAboutUsData() {
        AjaxResult ajaxResult = AjaxResult.success();
        JiayiAboutUsDTO companyProfile = aboutUsService.getInfoByKey("company_profile", null);
        JiayiAboutUsDTO devHistory = aboutUsService.getInfoByKey("dev_history", null);
        JiayiAboutUsDTO companyHonor = aboutUsService.getInfoByKey("company_honor", null);
        return ajaxResult
                .put("companyProfile", companyProfile.getValueObj())
                .put("devHistory", devHistory.getValueObj())
                .put("devHistory", devHistory.getValueObj())
                .put("companyHonor", companyHonor.getValueObj());

    }

    /**
     * 联系我们相关内容
     */
    @GetMapping("/index/call-me-data")
    @ResponseBody
    public AjaxResult getCallMeData() {
        AjaxResult ajaxResult = AjaxResult.success();
        JiayiAboutUsDTO callUs = aboutUsService.getInfoByKey("call_us", null);
        return ajaxResult.put("callUs", callUs.getValueObj());
    }

    /**
     * 获取产品列表
     */
    @PostMapping("/index/get-product")
    @ResponseBody
    public AjaxResult getProduct(@RequestBody JiayiProductSearchPageRequest request) {
        TableDataInfo tableDataInfo = jiayiProductService.selectJiayiProductList(request);
        return AjaxResult.success(tableDataInfo.getRows()).put("total", tableDataInfo.getTotal());
    }

    /**
     * 获取新闻
     */
    @PostMapping("/index/get-news")
    @ResponseBody
    public AjaxResult getNews(@RequestBody JiayiNewsSearchPageRequest request) {
        TableDataInfo tableDataInfo = jiayiNewsService.selectJiayiNewsList(request);
        return AjaxResult.success(tableDataInfo.getRows()).put("total", tableDataInfo.getTotal());
    }

}
